package com.bestcem.xm.user.grpc.service;

import com.bestcem.bp.xm.user.grpc.v1.services.*;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.user.grpc.service.converter.UserSmtpConverter;
import com.bestcem.xm.user.service.UserSmtpService;
import com.bestcem.xm.user.service.dto.user.UserSmtpDTO;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户邮箱配置grpc服务
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/17 14:18
 */
@Slf4j
@GrpcService
public class UserSmtpGrpcService extends SmtpServiceGrpc.SmtpServiceImplBase {

    @Resource
    private UserSmtpService userSmtpService;

    /**
     * 根据邮箱配置id查询邮箱配置信息
     *
     * @param request          请求信息
     * @param responseObserver 响应内容
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/03/17 14:20
     */
    @Override
    public void getSmtpById(GetSmtpByIdRequest request, StreamObserver<GetSmtpByIdResponse> responseObserver) {
        // 获取返回对象的构造器
        GetSmtpByIdResponse.Builder builder = GetSmtpByIdResponse.newBuilder();

        // 参数校验
        if (StringUtils.isBlank(request.getId())) {
            builder.setStatus(RpcStatusResult.error("orgId不能为空"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        // 根据邮箱配置id查询邮箱配置信息
        ServiceResult<UserSmtpDTO> result = userSmtpService.selectById(request.getId());
        if (!result.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        } else {
            builder.setStatus(RpcStatusResult.success());
            builder.setSmtp(UserSmtpConverter.dtoToSmtp(result.getData()));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 根据相关条件查询邮箱配置列表信息
     *
     * @param request          请求信息
     * @param responseObserver 响应内容
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/03/17 14:21
     */
    @Override
    public void listSmtps(ListSmtpsRequest request, StreamObserver<ListSmtpsResponse> responseObserver) {
        // 获取返回对象的构造器
        ListSmtpsResponse.Builder builder = ListSmtpsResponse.newBuilder();

        // 根据相关参数查询邮箱配置信息
        SmtpConditon smtp = request.getSmtp();
        UserSmtpDTO userSmtpDTO = UserSmtpConverter.smtpToDto(smtp);

        // 查询邮箱相关配置信息
        ServiceResult<List<UserSmtpDTO>> result = userSmtpService.selectBySelective(userSmtpDTO);
        if (!result.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        } else {
            builder.setStatus(RpcStatusResult.success());
            builder.addAllSmtps(UserSmtpConverter.dtosToSmtps(result.getData()));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

}
